package com.yoomiito.app.adapter.goods.detail;

import android.content.Context;
import androidx.annotation.NonNull;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.yoomiito.app.R;
import com.yoomiito.app.listener.ItemClickListener;
import com.yoomiito.app.model.bean.GoodsDetail;
import com.yoomiito.app.model.bean.MultipleItem;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.SpannableUtils;

import java.util.List;

public class GoodsDetailRecommendAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private List<GoodsDetail> mGoodsDetails;
    private ItemClickListener mItemClickListener;
    public GoodsDetailRecommendAdapter(Context context, LayoutHelper layoutHelper, List<GoodsDetail> goodsDetails) {
        mContext = context;
        mLayoutHelper = layoutHelper;
        mGoodsDetails = goodsDetails;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_goods, parent,false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        GoodsDetail detail = mGoodsDetails.get(position);
        ImageLoader.getInstance().into(mContext, detail.getPict_url(),viewHolder.goodsPic);
        viewHolder.goodsName.setText(detail.getTitle());
        viewHolder.goodsPrice.setText(detail.getSalePrice());
        viewHolder.saleSize.setText("销量" + detail.getVolume());
        viewHolder.storeName.setText(detail.getNick());
        if (TextUtils.isEmpty(detail.getCoupon_click_url())){
            viewHolder.mCouponView.setVisibility(View.GONE);
        }
        viewHolder.mView.setOnClickListener(v -> {
            if (mItemClickListener!=null){
                mItemClickListener.onItemClick(position);
            }
        });
    }

    private void setGiftAndCoupon(ImageView iv, View view, TextView tv, String value) {
//        boolean isCoupon = TextUtils.isEmpty(value)||"0".equals(value)||"0.0".equals(value)||"0.00".equals(value);
        boolean isCoupon = SpannableUtils.isEmptyOr0(value);
        iv.setVisibility(isCoupon ? View.GONE : View.VISIBLE);
        view.setVisibility(isCoupon ? View.GONE : View.VISIBLE);
        if (!isCoupon) tv.setText("¥" + value);
    }

    @Override
    public int getItemCount() {
        return mGoodsDetails.size();
    }

    @Override
    public int getItemViewType(int position) {
        return MultipleItem.RECOMMEND;
    }

    public ItemClickListener getItemClickListener() {
        return mItemClickListener;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder{

        private TextView saleSize;
        private TextView storeName;
        private TextView goodsPrice;
        private TextView giftTv_2;
        private LinearLayout giftLl_2;
        private ImageView giftIv_2;
        private TextView giftTv_1;
        private LinearLayout giftLl_1;
        private ImageView gfitIv_1;
        private TextView goodsName;
        private ImageView goodsPic;
        private CardView mView;
        private ConstraintLayout mCouponView;
        public ItemViewHolder(View itemView) {
            super(itemView);
            mView = itemView.findViewById(R.id.item_goods_cardView);
            mCouponView = itemView.findViewById(R.id.item_coupon_view);
            goodsPic = itemView.findViewById(R.id.item_goods_img);
            goodsName = itemView.findViewById(R.id.goodsTitle);
            gfitIv_1 = itemView.findViewById(R.id.item_gift_iv_1);
            giftLl_1 = itemView.findViewById(R.id.item_gift_ll_1);
            giftTv_1 = itemView.findViewById(R.id.tv_bonus);
            giftIv_2 = itemView.findViewById(R.id.item_coupon_iv_2);
            giftLl_2 = itemView.findViewById(R.id.item_coupon_ll_2);
            giftTv_2 = itemView.findViewById(R.id.tv_voucher);
            goodsPrice = itemView.findViewById(R.id.goodsPrice);
            storeName = itemView.findViewById(R.id.item_store_name);
            saleSize = itemView.findViewById(R.id.item_sale_size);
        }
    }
}
